package com.abeir.bilibili.utils;

import java.io.IOException;
import java.nio.file.Path;

public class ProgramExecutor {

    private final ProcessBuilder builder = new ProcessBuilder();

    public void setWorkingDirectory(Path dir){
        builder.directory(dir.toFile());
    }

    public void setEnableOutputConsole(boolean enable){
        if(enable){
            builder.redirectErrorStream(true);
            builder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
        }
    }

    public int exec(String... cmd) throws IOException, InterruptedException {
        builder.command(cmd);
        Process process = builder.start();
        return process.waitFor();
    }
}
